﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Manager.App_Code;
using System.Linq;

namespace Manager
{
    public partial class Department : UserControl
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();
        public int ID
        {
            get;
            set;
        }
        public int Position
        {
            get;
            set;
        }
        public Room[] Rooms = new Room[0];
        public string Name
        {
            get { return _btnName.Text; }
            set { _btnName.Text = value; }
        }
        
        public Department()
        {
            InitializeComponent();
        }

        public Department(string name)
        {
            InitializeComponent();
            Name = name;
        }

        public void ShowRooms()
        {
            LoadRooms();
            this.Width = 60 + 155 * Rooms.Length;
            for (int j = 0; j < this.Controls.Count; ++j)
            {
                if (!Controls[j].GetType().Equals(_btnName.GetType()))
                {
                    Controls.RemoveAt(j);
                }
            }
            for (int i = 0; i < Rooms.Length; ++i)
            {
                Rooms[i].Location = new System.Drawing.Point(65 + i * 155, 1);
                //Controls.Add(Rooms[i]);
            }
            Controls.AddRange(Rooms);
        }       
        
        public void AddRoom()
        {
            FrmAddRoom frmAddRoom = new FrmAddRoom();
            DialogResult dr = frmAddRoom.ShowDialog();
            if (dr == DialogResult.OK)
            {
                ShowRooms();
            }
        }

        public void LoadRooms()
        {
            var lstPhong = db.Phongs.Where(i => i.MaTang == ID).ToList();
            if (lstPhong.Count > 0)
            {
                Rooms = new Room[lstPhong.Count];
                for (int i = 0; i < lstPhong.Count; ++i)
                {
                    Rooms[i] = new Room();
                    Rooms[i].MaPhong = lstPhong[i].MaPhong;
                    Rooms[i].TenPhong = lstPhong[i].TenPhong;
                    var temp = db.LoaiPhongs.Where(t => t.MaLoaiPhong == lstPhong[i].LoaiPhong).ToList();
                    if (temp.Count > 0)
                    {
                        var loaiPhong = temp.First();
                        Rooms[i].LoaiPhong = loaiPhong.TenLoaiPhong;
                    }
                }
            }
        }

        private void newRoomToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //AddRoom();
            //ShowRooms();
        }

        private void _btnName_Click(object sender, EventArgs e)
        {
            //AddRoom();
            //ShowRooms();
        }

        private void _removeDepToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //DataDepartment ddep = new DataDepartment();
            //ddep.MaTang = ID;
            //ddep.Delete(ddep);
        }

      
    }
}
